package web.jsoft.com;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@ManagedBean
@SessionScoped
public class GoogleIdentityBean {
	private UserService userService;
	
	public GoogleIdentityBean(){
		userService = UserServiceFactory.getUserService();
	}
	
	public String getLoginURL() {
		ExternalContext ctx = FacesContext.getCurrentInstance().getExternalContext();
		HttpServletRequest request = (HttpServletRequest) ctx.getRequest();
		HttpServletResponse response = (HttpServletResponse) ctx.getResponse();
	      
		return userService.createLoginURL(response.encodeURL(request.getRequestURI()));
	}
	
	public String getLogoutURL() {
		return userService.createLogoutURL("/");
	}
	
	public User getCurrentUser() {		
		return userService.getCurrentUser();
	} 
	
	public boolean isAuthenticated() { 
		return userService.isUserLoggedIn();		
    }
	
	public boolean isAdmin() { 
		if (userService.isUserLoggedIn()){
			return userService.isUserAdmin();	
		}
		else {
			return false;
		}
    } 
}
